package com.qsd.common.util;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.Map;

public class Myquery {

    public static <T> Page<T> builder(Map<String,Object> param, BaseMapper<T> mapper){


        Long currentPage=1L;
        Long pageSize=10L;
        QueryWrapper<T> tQueryWrapper = new QueryWrapper<>();
        for (Map.Entry<String, Object> stringObjectEntry : param.entrySet()) {
            String key = stringObjectEntry.getKey();
            Object value = stringObjectEntry.getValue();
            if (StringUtils.equals("currentPage",key)) {
                currentPage= (Long) value;
            }else if (StringUtils.equals("pageSize",key)){
                pageSize= (Long) value;
            }else{
                //eq$name  like$name
                String[] split = key.split("\\$");
                switch (split[0]){
                    //select name from stu;
                    case "like":
                        tQueryWrapper.like(split[1],value);
                        break;

                    case "likeLeft":
                        tQueryWrapper.likeLeft(split[1],value);
                        break;
                    case "likeRight":
                        tQueryWrapper.likeRight(split[1],value);
                        break;
                    case "ge":
                        tQueryWrapper.ge(split[1],value);
                        break;

                    case "le":
                        tQueryWrapper.le(split[1],value);
                        break;
                    case "eq":
                    default:
                        tQueryWrapper.eq(split[1],value);
                }

            }
        }
        return mapper.selectPage(new Page<>(currentPage,pageSize),tQueryWrapper);
    }


}
